<template>
	<view class="wrap">
		<u-toast ref="uToast" />
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录在线选座系统</view>
			<u-form :model="loginForm" ref="loginFormRef">
				<u-form-item label="用户名" prop="username" label-width="150">
					<u-input v-model.number="loginForm.username" class="u-border-bottom" placeholder="请输入学号" />
				</u-form-item>
				<u-form-item label="密码" prop="password" label-width="150">
					<u-input type="password" v-model="loginForm.password" class="u-border-bottom" placeholder="请输入密码" />
				</u-form-item>
				<view class="tips">若显示该用户不存在请联系管理员添加</view>
			</u-form>
			<button @click="login()" :loading="buttonLoading" :style="[inputStyle]" class="getCaptcha">登录</button>
		</view>
		<view class="buttom">
			<view class="hint">
				若忘记密码，请联系
				<text class="link">管理员，电话：13812341234</text>
				重置密码
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				buttonLoading: false,
				loginForm: {
					username: '2',
					password: '123456',
				},
				loginRules: {
					username: [{
							required: true,
							message: '学号不能为空',
							trigger: "change"
						},
						{
							type: 'number',
							message: '学号必须为数字值'
						}
					],
					password: [{
						required: true,
						message: "请输入密码",
						trigger: "change"
					}]
				}
			};
		},
		computed: {
			//登录按钮样式
			inputStyle() {
				let style = {};
				if (this.loginForm.password) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			login() {
				this.$refs.loginFormRef.validate(valid => {
					if (!valid) return
					this.buttonLoading = true;
					uni.request({
							url: "http://192.168.2.149:3000/user/login",
							method: "POST",
							data: this.loginForm,
						})
						.then((res) => {
							console.log(res)
							if (res[1].data.status === 200) {
								console.log(111)
								uni.setStorage({
										key: "userInfo",
										data: res[1].data.data,
									}),
									uni.setStorage({
										key: "userStatus",
										data: res[1].data.data.status,
									}),
									this.$refs.uToast.show({
										title: '登录成功',
										type: 'success',
										position: 'top',
									})
								uni.switchTab({
									url: '/pages/Login/Home'
								});
							} else if (res[1].data.status === 408) {
								this.$refs.uToast.show({
									title: '没有登录权限',
									type: 'error',
									position: 'top',
								})
							} else if (res[1].data.status === 409) {
								this.$refs.uToast.show({
									title: '正被禁用状态',
									type: 'error',
									position: 'top',
								})
							} else if (res[1].data.status === 422) {
								this.$refs.uToast.show({
									title: '用户不存在',
									type: 'error',
									position: 'top',
								})
							} else if (res[1].data.status === 423) {
								this.$refs.uToast.show({
									title: '密码错误',
									type: 'error',
									position: 'top',
								})
							} else {
								this.$refs.uToast.show({
									title: '暂时无法登录',
									type: 'error',
									position: 'top',
								})
							}
							this.buttonLoading = false;
						})
				});
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.loginFormRef.setRules(this.loginRules);
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 350rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;
				text-align: center;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
